/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.ui;

import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.CollaborationUse;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.ui.dialogs.IChooseDialog;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.classdiagram.helper.EdgeDependencyHelper;

/**
 * Generic dialog that allows user to add/remove items to an element.
 * 
 * @author Patrick Tessier, Remi Schnekenburger
 */
public class RoleCreationDialog extends Dialog implements IChooseDialog {
	
	/**
	 * 
	 */
	protected Combo combo;
	
	/**
	 * 
	 */
	protected String[] elementList= {"test","test1","test2"};
	
	/**
	 * 
	 */
	protected Text nameField;
	
	/**
	 * 
	 */
	protected String announce="Choose your element";
	
	/**
	 * 
	 */
	protected static final int channel=19;
	
	/**
	 * 
	 */
	protected Object value=null;
	
	/**
	 * 
	 */
	protected List possibleRolePlayerList;
	
	/**
	 * 
	 */
	protected List possibleRoleList;

	/**
	 * 
	 */
	protected Label possibleSetText;
	
	/**
	 * 
	 */
	protected Label selectedElementText;
	
	/**
	 * 
	 */
	protected String possibleText="Choose Element";
	
	/**
	 * 
	 */
	protected String selectedText="Selected Element";
	
	/**
	 * 
	 */
	private CollaborationUse collaborationUse;
	
	/**
	 * 
	 */
	private static final Image IMG_UP_ARROW 
		= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ArrowUp_16x16.gif");
	
	/**
	 * 
	 */
	private static final Image IMG_DOWN_ARROW 
		= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ArrowDown_16x16.gif");
	
	/**
	 * 
	 */
	private static final Image IMG_LEFT_ARROW 
		= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ArrowLeft_16x16.gif");
	
	/**
	 * 
	 */
	private static final Image IMG_RIGHT_ARROW 
		= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ArrowRight_16x16.gif");
	
	
	
	
	/**
	 * 
	 * 
	 * @param parentShell 
	 * @param selectedText 
	 * @param possibleText 
	 */
	public RoleCreationDialog(Shell parentShell,
							String possibleText,
							String selectedText){
		super(parentShell);
		setShellStyle(SWT.RESIZE | super.getShellStyle());
		Debug.debug(this,"ChooseDialog", channel);
		if(possibleText!=null) {
			this.possibleText=possibleText; //set possible Text
		}
		if(selectedText!=null) {
			this.selectedText=selectedText;//set selected Label
		}
	
	}
	
	/**
	 * 
	 * 
	 * @param collaborationUse 
	 */
	public void setCollaborationUse(CollaborationUse collaborationUse){
		this.collaborationUse=collaborationUse;
	}
	
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	protected Control createDialogArea(Composite parent){
		//place all buttons
		Composite comp = (Composite) super.createDialogArea(parent);
		FormLayout layout = new FormLayout();
		comp.setLayout(layout);

		// initializes data
		FormData data;
		
		// create elements
		createElements(comp);
		
		// set fonts for elements that require fonts
		setFonts();
		
		// set data to the left elements: possible elements
		// Label: available elements title
		data = new FormData();
		data.top=new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		data.left=new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		possibleSetText.setLayoutData(data);
		
		// List: available elements list
		data =new FormData();
		data.height=200;
		data.width=300;
		data.top	= new FormAttachment(possibleSetText,ITabbedPropertyConstants.VSPACE);
		data.left	= new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		data.bottom = new FormAttachment(100, -ITabbedPropertyConstants.VSPACE);
		data.right 	= new FormAttachment(possibleRoleList, -ITabbedPropertyConstants.HSPACE);
		possibleRolePlayerList.setLayoutData(data);
		
		
		
		// Right part
		// Label: selected elements title 
		data 		= new FormData();
		
		data.top	= new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		data.left	= new FormAttachment(possibleRolePlayerList, ITabbedPropertyConstants.HSPACE);
		selectedElementText.setLayoutData(data);
		
		// Label: selected elements list
		data 		= new FormData();
		data.height=200;
		data.width=300;
		data.left	= new FormAttachment(possibleRolePlayerList, ITabbedPropertyConstants.HSPACE);
		data.top 	= new FormAttachment(selectedElementText, ITabbedPropertyConstants.VSPACE);
		//data.right 	= new FormAttachment(upButton, - ITabbedPropertyConstants.HSPACE);
		data.bottom	= new FormAttachment(100, -ITabbedPropertyConstants.VSPACE);
		possibleRoleList.setLayoutData(data);
		
		// refresh both lists
		fillSelectedElementList();
		fillPossibleElementList();
		
		// add listeners to button
		addButtonListeners();
		
		// end of the dialog
		Debug.debug(this,"createDialogArea", channel);	
		
		return comp;
	}
	
	/**
	 * Create elements in the  dialog.
	 * 
	 * @param comp the parent composite
	 */
	private void createElements(Composite comp) {
		// buttons
		
		// texts
		selectedElementText= new Label(comp, SWT.CENTER);
		selectedElementText.setText(selectedText);
		
		possibleSetText= new Label(comp, SWT.CENTER);
		possibleSetText.setText(possibleText);
		
		// lists
		possibleRoleList = new List(comp, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
		possibleRolePlayerList = new List(comp, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
	}

	/**
	 * Apply Font to elements that display text.
	 */
	private void setFonts() {
		FontData[] fontdata={new FontData("Arial", 9, SWT.NORMAL)};
		selectedElementText.setFont(PapyrusPlugin.fontManager.get(fontdata));
		possibleRoleList.setFont(PapyrusPlugin.fontManager.get(fontdata));
		possibleSetText.setFont(PapyrusPlugin.fontManager.get(fontdata));
		possibleRolePlayerList.setFont(PapyrusPlugin.fontManager.get(fontdata));
	}

	/**
	 * Add listeners to buttons.
	 */
	private void addButtonListeners() {
		
	}
	
	/**
	 * 
	 * 
	 * @param parent 
	 */
	protected void createComposite(Composite parent){
		Composite comp= new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout());
	}
	
	/**
	 * fill the possible list.
	 */
	protected void fillPossibleElementList(){
		Iterator<Property> propIterator=((Classifier)collaborationUse.getOwner()).getAllAttributes().iterator();
		while(propIterator.hasNext()){
			possibleRolePlayerList.add(propIterator.next().getLabel());
		}
	}
	
	/**
	 * fill the selected list.
	 */
	protected void fillSelectedElementList(){
		
		Iterator<Property> propIterator=collaborationUse.getType().getAllAttributes().iterator();
		while(propIterator.hasNext()){
			possibleRoleList.add(propIterator.next().getLabel());
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent){
		super.createButtonsForButtonBar(parent);	
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 */
	/**
	 * 
	 * 
	 * @param buttonId 
	 */
	@Override
	protected void buttonPressed(int buttonId){
		super.buttonPressed(buttonId);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
	 */
	/**
	 * 
	 */
	@Override
	protected void okPressed(){
		Debug.debug(this,"action...", channel);
		Property role=(Property)collaborationUse.getType().getAllAttributes().get(possibleRoleList.getSelectionIndex());
		Property rolePlayer=(Property)((Classifier)collaborationUse.getOwner()).getAllAttributes().get(possibleRolePlayerList.getSelectionIndex());
		
		EdgeDependencyHelper edh= new EdgeDependencyHelper();
		Dependency dependency = UMLFactory.eINSTANCE.createDependency();
		edh.connect(rolePlayer, role, dependency);
		collaborationUse.getRoleBindings().add(dependency);
		value=dependency;
		super.okPressed();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
	 */
	/**
	 * 
	 */
	@Override
	protected void cancelPressed() {
		value=null;
		super.cancelPressed();
		
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.IChooseDialog#getValue()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	public Object getValue(){
		return value;
	}

	

}
